package com.example.armmvvm.ui.customdraw.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.example.arm.ext.dp
import timber.log.Timber

/**
 * 绘制文字
 *
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.custom
 * @createTime 创建时间 ：2021/9/16
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class DrawTextTestView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
    }

    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.BLUE
        typeface = Typeface.SANS_SERIF
    }

    private val fontMetrics = Paint.FontMetrics()

    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        paint.color = Color.GRAY
        canvas.drawRect(0f, 0f, width / 2f, height / 2f, paint)
        paint.color = Color.RED
        canvas.drawRect(width / 2f, 0f, width.toFloat(), height / 2f, paint)
        paint.color = Color.YELLOW
        canvas.drawRect(0f, height / 2f, width / 2f, height.toFloat(), paint)
        paint.color = Color.GREEN
        canvas.drawRect(width / 2f, height / 2f, width.toFloat(), height.toFloat(), paint)

        val str = "aBp事情"

        //-------------------绘制文字到中心
        // 纵向绘制中心为baseline线
        textPaint.textSize = 50.dp
        textPaint.textAlign = Paint.Align.CENTER
        textPaint.getFontMetrics(fontMetrics)
        canvas.drawText(str, width.toFloat() / 2, height.toFloat() / 2, textPaint)
        // 对于静态文字,应通过getTextBounds获取一段文字的绘制区域,通过顶部和底部计算中心位置
        // bounds:top-baseline到绘制文字的顶部距离,为负值;
        //        bottom-baseline到绘制文字的底部距离,为正值
        // 纵向绘制中心为baseline线,所以纵向移动的距离应该是(abs(top)-bottom)/2
        val bounds = Rect()
        textPaint.textSize = 18.dp
        textPaint.textAlign = Paint.Align.LEFT
        textPaint.getTextBounds(str, 0, str.length, bounds)
        Timber.d(bounds.toString())
        canvas.drawText(str, 0f, height.toFloat() / 2 - (bounds.top + bounds.bottom) / 2f, textPaint)
        // 对于动态态文字,应将baseline和(ascent - descent)的中心作为文字纵向中心,尤其是多类型文字
        // 注意ascent/top是负值,文字距离ascent是有一段距离的,如果取baseline和ascent的中心为文字纵向中心,文字会偏下
        // 默认的字体为Typeface DEFAULT,这个字体ascent == top,bottom == descent
        textPaint.textSize = 18.dp
        textPaint.textAlign = Paint.Align.LEFT
        textPaint.getFontMetrics(fontMetrics)
        Timber.d(fontMetrics.top.toString())
        Timber.d(fontMetrics.ascent.toString())
        Timber.d(fontMetrics.descent.toString())
        Timber.d(fontMetrics.bottom.toString())
        Timber.d(fontMetrics.leading.toString())
        canvas.drawText(
            str,
            bounds.right + 10.dp,
            height.toFloat() / 2 - (fontMetrics.ascent + fontMetrics.descent) / 2f,
            textPaint
        )

        // -------------------绘制文字到顶部
        // 调高字体大小,很明显看到文字距离ascent是有一段距离的,留有空隙,字体越大越明显
        textPaint.textSize = 50.dp
        textPaint.textAlign = Paint.Align.LEFT
        textPaint.getFontMetrics(fontMetrics)
        Timber.d(fontMetrics.top.toString())
        Timber.d(fontMetrics.ascent.toString())
        Timber.d(fontMetrics.descent.toString())
        Timber.d(fontMetrics.bottom.toString())
        Timber.d(fontMetrics.leading.toString())
        canvas.drawText(str, width / 2f, 0f - fontMetrics.ascent, textPaint)

        // 如果要字体贴顶部或左边,且不受字体大小影响,应使用bounds来计算字体实际绘制所需矩形空间
        textPaint.textSize = 50.dp
        textPaint.textAlign = Paint.Align.LEFT
        textPaint.getTextBounds(str, 0, str.length, bounds)
        Timber.d(bounds.toString())
        canvas.drawText(str, -bounds.left.toFloat(), -bounds.top.toFloat(), textPaint)

    }
}